package com.usian.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.errorprone.annotations.Var;
import com.usian.pojo.User;
import com.usian.query.UserDto;
import com.usian.service.UserService;
import com.usian.utils.PageResult;
import com.usian.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RefreshScope //动态从nacos配置中心获取最新值
public class UserController {

    @Value("${config.info}")
    private String configInfo;

    @Autowired
    private UserService userService;


    @RequestMapping("/list")
    public List<User> list(){
        System.out.println(configInfo);
        return userService.findAll();
    }

    /**
     * get请求 中/ 传参方式
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public User findById(@PathVariable Integer id){
        return userService.findById(id);
    }

    @GetMapping("/findByIds")
    public User findByIds(@RequestParam Integer id,@RequestParam String name){
        System.out.println(name);
        return userService.findById(id);
    }

    /**
     * 用户分页查询
     *
     * @param dto
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult<User> findPage(@RequestBody UserDto dto){
        PageResult<User> page = userService.findPage(dto);
        return page;
    }

    /**
     *  保存用户
     *
     * @param user
     * @return
     */
    @PostMapping("/saveUser")
    public boolean saveUser(@RequestBody User user){
        boolean result= false;
        if(user.getId()==null){
            result=userService.saveUser(user);
        }else{
            result=userService.updateUser(user);
        }
        return result;
    }

    /**
     * 删除用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public boolean deleteById(@RequestParam Integer id){
        return userService.deleteById(id);
    }

    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    @RequestMapping("/login")
    public User login(@RequestBody User user, HttpSession session){

        User user1= userService.login(user);
        return user1;
    }

}
